package com.neutech.base;

import com.neutech.runtime.*;
import com.neutech.util.ImageUtil;

import java.awt.*;
import java.awt.event.*;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CopyOnWriteArrayList;

public class GameFrame extends Frame {

    public GameFrame() {
        init();
    }

    private void init() {
        //设置窗口尺寸
        setSize(Constant.WINDOW_WIDTH, Constant.WINDOW_HEIGHT);
        //设置窗口居中
        setLocationRelativeTo(null);
        //设置禁止改变窗口大小
        setResizable(false);
        //设置禁止输入法
        enableInputMethods(false);
        //设置显示
        setVisible(true);
        //设置窗口关闭事件
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        //设置定时刷新
        new Thread() {
            public void run() {
                while (true) {
                    // 调用重绘
                    repaint();
                    try {
                        Thread.sleep(1000 / Constant.FPS);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }.start();
        //给窗口添加鼠标事件
        addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                if (gameStatus == GameStatusEnum.RUNNING) {
                    bird.resetY();
                } else if (gameStatus == GameStatusEnum.RUNNING) {
                    // 重开
                    reStart();
                }
            }
        });
        addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_SPACE) {
                    bird.resetY();
                }
            }
        });
        createPencial();
    }

    private void createPencial() {
    }

    Background background = new Background();

    Land land = new Land();

    public List<Pencil> pencilList = new CopyOnWriteArrayList<>();

    Bird bird = new Bird(Constant.WINDOW_WIDTH / 6, Constant.WINDOW_HEIGHT / 2);

    StartButton sb = new StartButton();

    Score score = new Score();

    private GameStatusEnum gameStatus = GameStatusEnum.RUNNING;

    @Override
    public void paint(Graphics g) {
        if (gameStatus == GameStatusEnum.RUNNING) {
            background.draw(g);
            // 先移动后绘制
            isAddPencil();
            for (Pencil pencil : pencilList) {
                pencil.move();
                pencil.draw(g);
            }

            land.move();
            land.draw(g);
            score.draw(g);
            bird.move();
            bird.draw(g);

            birdCollisionLand();
            birdCollisionPencil();
            isAddScore();
        } else if (gameStatus == GameStatusEnum.FAILED) {
            background.draw(g);
            for (Pencil pencil : pencilList) {
                pencil.draw(g);
            }
            land.draw(g);
            bird.draw(g);
            sb.draw(g);
            score.draw(g);
        }

    }

    public boolean isAddScore = true;

    public void isAddScore() {
        if (isAddScore && bird.getX() > pencilList.get(0).getX() + pencilList.get(0).getWidth()) {
            score.addScore();
            isAddScore = false;
        }
    }
    public void reStart() {
        // 铅笔集合清空
        pencilList.clear();
        // 添加一对儿初始铅笔
        createPencil();
        // 小鸟位置复原
        bird.resetSelf();
        score.reset();

        gameStatus = GameStatusEnum.RUNNING;
    }
    public void birdCollisionLand() {
        if (bird.getY() + bird.getHeight() >= land.getY()) {
            gameStatus = GameStatusEnum.FAILED;
        }
    }
    public void birdCollisionPencil() {
        for (Pencil pencil : pencilList) {
            if (pencil.createRectangle().intersects(bird.createRectangle())) {
                gameStatus = GameStatusEnum.FAILED;
            }
        }
    }
    Random random = new Random();

    public void isAddPencil() {
        if (pencilList.size() == 2 && pencilList.get(0).getX() <= Constant.WINDOW_WIDTH / 2 - 26) {
            createPencil();
        }
    }
    public void createPencil() {
        // minTop最小的top值
        int minTop = Constant.WINDOW_HEIGHT / 8 +
                Constant.WINDOW_TOOLBAR_HEIGHT;
        // 最大top值
        int maxTop = Constant.WINDOW_HEIGHT / 2;
        int gap = (Constant.WINDOW_HEIGHT / 5) + (Constant.WINDOW_TOOLBAR_HEIGHT / 2);
        int top = random.nextInt(maxTop - minTop) + minTop;

        pencilList.add(new PencilUp(Constant.WINDOW_WIDTH, top -
                ImageUtil.get("pu").getHeight(null)));
        pencilList.add(new PencilDown(Constant.WINDOW_WIDTH, top + gap));
    }
    private Image image;

    public void update(Graphics g) {
        // 截取窗体所在位置的图片
        if (image == null) {
            image = this.createImage(this.getWidth(), this.getHeight());
        }
        // 获得截取图片的画布
        Graphics gre = image.getGraphics();
        // 获取画布的底色并且使用这种颜色填充画布（默认的颜色为黑色）
        gre.setColor(gre.getColor());
        // 有清除上一步图像的功能，相当于gImage.clearRect(0, 0, WIDTH, HEIGHT)
        gre.fillRect(0, 0, this.getWidth(), this.getHeight());
        // 将截下的图片上的画布传给重绘函数，重绘函数只需要在截图的画布上绘制即可，不必在从底层绘制
        paint(gre);
        //将接下来的图片加载到窗体画布上去，才能考到每次画的效果
        g.drawImage(image, 0, 0, null);
    }
}



